<?php
namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Lib\Search;

class RoomHot extends Base
{
    //房间热度缓存
    protected $roomHotKey = 'ROOMHOT:';//string ROOMHOT:{room_id} => {num}

    protected $redis_has_ret_hot_ids = 'ROOMHOT:HAS_RET_IDS';
    protected $redis_all_room_hot_id_data = 'ROOM:ALL:ROOM:HOT:ID:DATA';
    private $search; //add by zxiaofee

    public function __construct(array $attributes = [])
    {
        $this->search = new Search();

        parent::__construct($this->table, $attributes);
    }

    /**
     * 设置房间热度值
     */
    public function set($roomId, $hotNum){
        $roomHotKey = $this->roomHotKey.$roomId;
        Redis::SET($roomHotKey, $hotNum);
        Redis::EXPIRE($roomHotKey, 86400);

        return true;
    }

    /**
     * 获取房间热度
     */
    public function get($roomId){
        $roomHotKey = $this->roomHotKey.$roomId;
        $hotNum = Redis::GET($roomHotKey);

        return intval($hotNum);
    }


    /**
     * 删除热度排行
     * @param $room_id
     * @param $type
     */
    public function del($roomId){
        $roomHotKey = $this->roomHotKey.$roomId;
        Redis::DEL($roomHotKey);
    }

    public function getHasRetHotIdsToUser($uid, $typeId){
        return Redis::HGET($this->redis_has_ret_hot_ids . ":" . $uid, $typeId);
    }

    public function setHasRetHotIdsToUser($uid, $typeId, $sid){
        Redis::HSET($this->redis_has_ret_hot_ids . ":" . $uid, $typeId, $sid);
        Redis::EXPIREAT($this->redis_has_ret_hot_ids . ":" . $uid, time() + 3600 * 24);
        return;
    }

    public function getAllRoomHotIdData(){
        return Redis::GET($this->redis_all_room_hot_id_data);
    }

    public function setAllRoomHotIdData($data){
        return Redis::SET($this->redis_all_room_hot_id_data, $data);
    }

    public function saveRoomHotToEs(Array $roomHot) {
        return $this->search->indexRoomHot($roomHot);
    }

    public function delRoomHotFromEs(Array $exceptRoomIds) {
        return $this->search->delRoomHot($exceptRoomIds);
    }

    public function queryRoomHotFromEs($query,$page = 0, $size = 20){
        return $this->search->queryRoomHot($query, $page, $size);
    }

    public function queryRoomHotIdsFromEs($query){
        return $this->search->queryRoomHotIds($query);
    }

}
